import * as fs from "fs"
import { Util } from "./Util";
import { ShellUtil } from "./ShellUtil";
import { DirUtil } from "./DirUtil";
import { Main } from "../Main";
import { FileIoUtil } from "./FileIOUtil";
/**
 * 根据png查找所有ans
 * @author hz
 */

type ExportMap = {
    pic:{[key:string]:true}
}

export class CheckUnQuotePngByAnsUtil {

    /**
     * 根据png查找所有ans
     * @param path 路径
     */
    static do(path:string):void {
        
        let pngFiles = DirUtil.getAll(path);

        let ansPath = `${Main.projPath}public/art/cocosstudio/cocosstudio/neAni/ans/`
        let ansFiles = DirUtil.getAll(ansPath);

        let pngList: any = {};
        let count1 = 0;
        pngFiles.forEach(pngPath => {
            var stat = fs.lstatSync(path+pngPath);
            if (stat.isDirectory() == false) {
                let temp1 = pngPath.lastIndexOf(".");//取到文件名开始到最后一个点的长度
                let path11 = pngPath.substring(0, temp1);//截

                pngList[path11] = [];

                let count2 = 0;
                for (const key in ansFiles) {
                    if (ansFiles.hasOwnProperty(key)) {
                        const element = ansFiles[key];
                        var stat = fs.lstatSync(ansPath+element);
                        if (stat.isDirectory() == false) {

                            FileIoUtil.readFile(ansPath+element, (data: Buffer)=>{
        
                                if (data.toString().includes(pngPath) == true) {
                                    pngList[path11].push(ansPath+element);
                                }

                                count2++;

                                if (count2 == ansFiles.length) {
                                    count1++;
                                    console.log(path11, `${count1/pngFiles.length*100}%`);
                                    if (count1 == pngFiles.length) {
                                        for (const key in pngList) {
                                            console.log("\n");
                                            let num = 1
                                            if (pngList.hasOwnProperty(key)) {
                                                const element = pngList[key];
                                                if (num == 1) {
                                                    console.log(key);
                                                }
                                                num++;
                                                console.log(element);
                                            }
                                        }
                                    }
                                } 
        
                            })
                            
                        } else {
                            count2++;
                        }
                    }
                }
            } else {
                count1++;
            }

        });

    }

    
}